package com.timoon.component.panel.jqgrid.action;

import java.util.Map;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.form.Form;

import com.googlecode.wicket.jquery.ui.Options;
import com.googlecode.wicket.jquery.ui.form.button.AjaxButton;
import com.timoon.component.behavior.jbox.AbstractJboxBehavior;
import com.timoon.component.panel.common.utils.ValidatorFeedbackPanel;
import com.timoon.component.panel.jqgrid.JqGridPanel;
import com.timoon.component.panel.jqgrid.JqGridService;

public abstract class SaveButton extends AjaxButton {
	private static final long serialVersionUID = -2220836380026371739L;

	private JqGridService service;
	
	private ModalWindow win;
	
	private String key;//grid的table标签id key
	
	/**
	 * 
	 * @param id
	 * @param formClass 对应的form的class属性，此属性是creatorPanel实例化form时随机生成
	 * @param win	存在关闭模态窗口动作
	 * @param service	保存数据
	 */
	public SaveButton(String id,JqGridPanel mainPanel) {
		super(id);
		// TODO Auto-generated constructor stub
		this.win=mainPanel.getWin();
		this.key=mainPanel.getKey();
		this.service=mainPanel.getService();
		
		this.add(new AbstractJboxBehavior("", "click", new Options()));
	}
	@Override
	protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
		// TODO Auto-generated method stub
		super.onSubmit(target, form);
		
		Map<String,Object> data=this.getInputData();
		
		ValidatorFeedbackPanel feedback=this.getFeedback();
		feedback.setData(data);
		
		if(feedback.isTested()){
			win.close(target);
			service.saveOrUpdateRecord(data);
			
			target.appendJavaScript("jBox.tip('保存成功','info');myjqgrid.reload('#grid-"+key+"');");
		}else{
			target.add(feedback);
		}
		
	}
	/**
	 * 单击【保存】按钮后，从此处向CreatorPanel取出输入值
	 * */
	protected abstract Map<String,Object> getInputData();
	
	protected abstract ValidatorFeedbackPanel getFeedback();
	
}
